﻿using SolidWorks.Interop.sldworks;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SolidworksDLL
{
    public class ComponentHelp
    {
        public static Component2[] GetAllComponents(AssemblyDoc assemblyDoc)
        {
            int componentCount = assemblyDoc.GetComponentCount(false);
            if (componentCount == 0)
            {
                return null;
            }
            else
            {
                return (Component2[])assemblyDoc.GetComponents(false);
            }
        }

        public static CompData GetComponents(AssemblyDoc assemblyDoc)
        {
            ModelDoc2 swModel = (ModelDoc2)assemblyDoc;
            ConfigurationManager swConfMgr = swModel.ConfigurationManager;
            Configuration swConf = swConfMgr.ActiveConfiguration;
            Component2 swRootComp = (Component2)swConf.GetRootComponent();

            CompData compData = new CompData();
            compData.component = swRootComp;
            GetComponents(compData);

            return compData;
        }

        public static void GetComponents(CompData compData)
        {
            List<CompData> compDatas = new List<CompData>();
            compData.childrenComp = compDatas;

            object[] comps = (object[])compData.component.GetChildren();
            if (comps != null && comps.Length > 0)
            {
                foreach (Component2 item in comps)
                {
                    CompData compData1 = new CompData();
                    compData1.component = item;
                    compDatas.Add(compData1);
                    GetComponents(compData1);
                }
            }
        }
    }

    public class CompData
    {
        public Component2 component;
        //public int Id;
        //public int ParId;

        public List<CompData> childrenComp;
    }
}